package com.knife.changerecord.config;


import cn.hutool.core.thread.ThreadFactoryBuilder;

import java.util.Objects;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lth
 * @created 2023年07月13日 星期四 15:29
 */
public class ChangeRecordThreadPool {

    private static ThreadPoolExecutor changeRecordThreadPool;

    public static ThreadPoolExecutor getChangeRecordThreadPool() {
        if (Objects.isNull(changeRecordThreadPool)) {
            synchronized (ChangeRecordThreadPool.class) {
                if (Objects.isNull(changeRecordThreadPool)) {
                    int coreSize = 0;
                    int maxSize = 2;
                    changeRecordThreadPool = new ThreadPoolExecutor(
                            coreSize, maxSize,
                            30, TimeUnit.SECONDS,
                            new SynchronousQueue<>(),
                            ThreadFactoryBuilder.create().setNamePrefix("ChangeRecordThreadPool-%d").build(),
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                }
            }
        }
        return changeRecordThreadPool;
    }
}
